package com.aiwu.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.aiwu.bean.ScreenConfig;

import org.citra.citra_emu.R;

import java.util.ArrayList;
import java.util.List;

public class ScreenLayoutAdapter extends BaseAdapter {
    private List<ScreenConfig> mData;
    private BtnClickListener btnClickListener;

    public ScreenLayoutAdapter() {

    }

    public List<ScreenConfig> getData() {
        return mData;
    }

    public void setData(List<ScreenConfig> mData) {
        this.mData = mData;
        notifyDataSetChanged();
    }

    public void addData(ScreenConfig itemBean) {
        if (mData == null) {
            mData = new ArrayList<>();
        }
        mData.add(itemBean);
        notifyDataSetChanged();
    }

    public void removeData(int position) {
        if (mData == null) {
            return;
        }
        mData.remove(position);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public ScreenConfig getItem(int position) {
        return mData == null ? null : mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final Context context = parent.getContext();
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_operate_config, null);
            holder.tvName = convertView.findViewById(R.id.tv_name);
            holder.ivEditName = convertView.findViewById(R.id.iv_edit);
            holder.layoutName = convertView.findViewById(R.id.layout_name);
            holder.btnUse = convertView.findViewById(R.id.btn_use);
            holder.btnEdit = convertView.findViewById(R.id.btn_edit);
            holder.btnCopy = convertView.findViewById(R.id.btn_copy);
            holder.btnDelete = convertView.findViewById(R.id.btn_delete);
            convertView.setTag(holder);
        }
        holder = (ViewHolder) convertView.getTag();
        final ScreenConfig item = mData.get(position);
        holder.tvName.setText(item.getName());
        holder.btnUse.setText(item.isUsing() ? context.getString(R.string.using) : context.getString(R.string.use));
        holder.btnUse.setEnabled(!item.isUsing());
        holder.btnUse.setOnClickListener(v -> {
            if (btnClickListener != null) {
                btnClickListener.useBtnClick(item.getId(), position);
            }
        });
        if (item.isCustom()) {
            holder.ivEditName.setVisibility(View.VISIBLE);
            holder.layoutName.setOnClickListener(v -> {
                if (btnClickListener != null) {
                    btnClickListener.nameBtnClick(item.getId(), position);
                }
            });

            holder.btnCopy.setVisibility(View.VISIBLE);
            holder.btnCopy.setOnClickListener(v -> {
                if (btnClickListener != null) {
                    btnClickListener.copyBtnClick(item.getId(), position);
                }
            });
            holder.btnEdit.setVisibility(View.VISIBLE);
            holder.btnEdit.setOnClickListener(v -> {
                if (btnClickListener != null) {
                    btnClickListener.editBtnClick(item.getId(), position);
                }
            });
        } else {
            holder.ivEditName.setVisibility(View.GONE);
            holder.layoutName.setOnClickListener(null);

            holder.btnCopy.setVisibility(View.GONE);
            holder.btnCopy.setOnClickListener(null);

            holder.btnEdit.setVisibility(View.GONE);
            holder.btnEdit.setOnClickListener(null);
        }

        if (item.isCustom() && !item.isUsing()) {
            holder.btnDelete.setVisibility(View.VISIBLE);
            holder.btnDelete.setOnClickListener(v -> {
                if (btnClickListener != null) {
                    btnClickListener.deleteBtnClick(item.getId(), position);
                }
            });
        } else {
            holder.btnDelete.setVisibility(View.GONE);
            holder.btnDelete.setOnClickListener(null);
        }
        return convertView;
    }

    public void setBtnClickListener(BtnClickListener btnClickListener) {
        this.btnClickListener = btnClickListener;
    }

    static class ViewHolder {
        TextView tvName;
        ImageView ivEditName;
        ViewGroup layoutName;
        Button btnUse;
        Button btnEdit;
        Button btnCopy;
        Button btnDelete;
    }

    public interface BtnClickListener {
        void nameBtnClick(int id, int position);

        void useBtnClick(int id, int position);

        void copyBtnClick(int id, int position);

        void editBtnClick(int id, int position);

        void deleteBtnClick(int id, int position);
    }
}
